<html>
 <head>
  <title>Test Page for Adapters</title>
  <script language="javascript" src="../../jsunit/app/jsUnitCore.js"></script>
  <script language="javascript" src="../compiled/ca-all.js"></script>
 </head>
 <body>
  <script language="javascript">
  	function setUp(){
		try {
			//Define interface
			CA.Interfaces.Create("ITrue", ["IsTrue"]);
			//Define interface
			CA.Interfaces.Create("IFalse", ["IsFalse"]);
			//Define interface
			CA.Interfaces.Create("ILie", ["IsReallyTrue"]);
		}catch(e){}				
	}
    function testAdaptation() {	
	    //Implements ITrue
	    function aTruth(val){
	        this.val = val;
	    }
	    aTruth.prototype.IsTrue = function(){ 
	        return this.val; 
	    };
	    //Create an adapter from ITrue to IFalse
	    var adaptITrueToIFalse = function(o){
	        return {
	            IsFalse : function(){ return !o.IsTrue.call(o); }
	        };
	    };
	    //Register the adapter with long name
	    CA.Adapters.Register("ITrue","IFalse",adaptITrueToIFalse);
		//Create instance
		implTruth = new aTruth(true);
	    //Adapt the implementation of IAnotherConsole to IConsole
	    var adaptedIFalse = CA.Adapters.Adapt(implTruth,"IFalse");
	    //Invoke the IConsole interface
		assertTrue("True prior to adaptation",implTruth.IsTrue());
		assertFalse("Adapted to false",adaptedIFalse.IsFalse());
    }
    function testFalseAdapter() {	
	    //Implements ITrue
	    function aTruth(val){
	        this.val = val;
	    }
	    aTruth.prototype.IsTrue = function(){ 
	        return this.val; 
	    };
	    //Create an adapter from ITrue to ILie
	    var adaptITrueToILie = function(o){
	        return {
	            IsFalse : function(){ return !o.IsTrue.call(o); }
	        };
	    };
	    //Register the adapter with long name
	    CA.Adapters.Register("ITrue","ILie",adaptITrueToILie);
		//Create instance
		implTruth = new aTruth(true);
		assertTrue("True prior to adaptation",implTruth.IsTrue());
		var succeed = true;
		try {
			var adaptedIFalse = CA.Adapters.Adapt(implTruth, "ILie",true);
		}catch(e){
			succeed = false;
		}		
		assertFalse("Adaptation should fail for delivering wrong interface",succeed);
    }
  </script>
 </body>
</html>